﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using DAL;

namespace BLL.Faq
{
    public class Contacts:BaseFaq
    {
        #region "Constructors"
        public Contacts() { }

        public Contacts(int contactID, DateTime addedDate, string addedBy, string address, string phone, string email, string subject, string question, DateTime answerDate, string answerBy, string answer, bool approved)
        {
            this.ID = contactID;
            this.AddedDate = addedDate;
            this.AddedBy = addedBy;
            this.Address = address;
            this.Phone = phone;
            this.Email = email;
            this.Subject = subject;
            this.Question = question;
            this.AnswerDate = answerDate;
            this.AnswerBy = answerBy;
            this.Answer = answer;
            this.Approved = approved;
        }
        #endregion

        #region "Public Properties"

        public string Address { get; set; }

        public string Phone { get; set; }

        public string Email { get; set; }

        public string Subject { get; set; }

        public string Question { get; set; }

        private DateTime _answerDate = DateTime.Now;
        public DateTime AnswerDate
        {
            get { return _answerDate; }
            protected set { _answerDate = value; }
        }

        public string AnswerBy { get; set; }

        public string Answer { get; set; }

        public bool Approved { get; set; }

        #endregion

        #region Get Contact From ContactsDetail
        private static Contacts GetContactFromContactsDetail(ContactsDetails record)
        {
            if (record == null)
                return null;
            else
                return new Contacts(record.ContactID, record.AddedDate, record.AddedBy, record.Address, record.Phone, record.Email, record.Subject, record.Question, record.AnswerDate, record.AnswerBy,record.Answer,record.Approved);
        }

        private static List<Contacts> GetContactFromListContactDetail(List<ContactsDetails> recordset)
        {
            List<Contacts> Contacts = new List<Contacts>();
            foreach (ContactsDetails record in recordset)
                Contacts.Add(GetContactFromContactsDetail(record));
            return Contacts;
        }
        #endregion

        #region Method
        public static Contacts GetContact(int ContactID)
        {
            Contacts Contact = null;
            string key = "Contact_" + ContactID.ToString();
            if (BaseFaq.Settings.EnableCaching && BizObject.Cache[key] != null)
                Contact = (Contacts)BizObject.Cache[key];
            else
            {
                Contact = GetContactFromContactsDetail(SiteProvider.Faq.ContactGet(ContactID));
                BaseFaq.CacheData(key, Contact);
            }
            return Contact;
        }

        public static List<Contacts> ListContact(int approved)
        {
            bool bApproved = false;
            if (approved == 0)
                return ListContact();
            if (approved == 1)
                bApproved = false;
            if (approved == 2)
                bApproved = true;
            List<Contacts> contacts = null;
            string key = "Contacts_" + bApproved.ToString();

            if (BaseFaq.Settings.EnableCaching && BizObject.Cache[key] != null)
            {
                contacts = (List<Contacts>)BizObject.Cache[key];
            }
            else
            {

                List<ContactsDetails> recordset = SiteProvider.Faq.ContactGetApproved(bApproved);
                contacts = GetContactFromListContactDetail(recordset);
                BaseFaq.CacheData(key, contacts);
            }
            return contacts;
        }

        public static List<Contacts> ListContact()
        {
            List<Contacts> Contacts = null;
            string key = "Contacts";
            if (BaseFaq.Settings.EnableCaching && BizObject.Cache[key] != null)
                Contacts = (List<Contacts>)BizObject.Cache[key];
            else
            {
                List<ContactsDetails> recordset = SiteProvider.Faq.ContactList();
                Contacts = GetContactFromListContactDetail(recordset);
                BaseFaq.CacheData(key, Contacts);
            }
            return Contacts;
        }

        public static int InsertContact(string address, string phone, string email, string subject, string question, DateTime answerDate, string answerBy, string answer, bool approved)
        {
            ContactsDetails record = new ContactsDetails(0, DateTime.Now,BizObject.CurrentUserName,address,phone,email,subject,question,answerDate,answerBy,answer,approved);
            int ret = SiteProvider.Faq.InsertContact(record);
            BizObject.PurgeCacheItems("Contact");
            return ret;
        }

        public static bool UpdateContact(int id, string address, string phone, string email, string subject, string question, DateTime answerDate, string answerBy, string answer, bool approved)
        {
            ContactsDetails record = new ContactsDetails(id, DateTime.Now, BizObject.CurrentUserName, address, phone, email, subject, question, DateTime.Now, BizObject.CurrentUserName, answer, approved);
            bool ret = SiteProvider.Faq.UpdateContact(record);
            BizObject.PurgeCacheItems("Contact");
            return ret;
        }

        public static bool DeleteContact(int id)
        {
            bool ret = SiteProvider.Faq.DeleteContact(id);
            BizObject.PurgeCacheItems("Contact");
            return ret;
        }
        #endregion
    }
}
